package logic

import (
	"context"
	"doctor-medicine-system/models/mysql"
	"doctor-medicine-system/pkg"
	"errors"

	"doctor-medicine-system/systemrpc/internal/svc"
	"doctor-medicine-system/systemrpc/systemrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 登录
func (l *LoginLogic) Login(in *systemrpc.LoginRequest) (*systemrpc.LoginResponse, error) {
	// todo: add your logic here and delete this line
	var user mysql.User
	err := user.FirstUserByWorker(mysql.DB, in.WorkCode)
	if err != nil {
		return nil, errors.New("登录自己的医生工号")
	}
	if user.ID == 0 {
		return nil, errors.New("工号不存在")
	}
	if pkg.Md5(in.Password) != user.Password {
		return nil, errors.New("密码不正确")
	}

	return &systemrpc.LoginResponse{
		UserId:   int64(user.ID),
		WorkCode: user.WorkCode,
		Password: user.Password,
	}, nil
}
